<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

       <el-col :lg="6" :md="12">
          <el-form-item label="统计学年:">
            <el-select
              filterable
              clearable
              v-model="where.annual"
              @change="$forceUpdate()"
              size="small"
              placeholder="-学年统计-"
              class="ele-block">
              <el-option v-for="item in xuenianList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="10" :md="12">
          <el-form-item label="统计日期:">
            <el-col :lg="8" :md="12">
              <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.startTime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择开始日期"/>
            </el-col>
            <el-col :lg="2" :md="12">
              至
            </el-col>
            <el-col :lg="8" :md="12">
              <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.endTime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择结束日期"/>
            </el-col>
          </el-form-item>
        </el-col>
       <el-col :lg="6" :md="12">
         <el-form-item label="项目:">
          <project-select  v-model="where.projectId" @chageProject="chageProject" @chageProjectId="chageProjectId"></project-select>
         </el-form-item>
       </el-col>
        <el-col :lg="6" :md="12">
          <el-form-item ref="class" prop="grade" label="年级:">
            <grade-select  v-model="where.gradeId"  @changeGrade="changeGrade" @changeGradeId="changeGradeId"></grade-select>
          </el-form-item>
        </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="院系类型:">
              <el-select
                clearable
                v-model="where.stype"
                placeholder="请选择院系类型"
                @change="getDepartmentsList"
                class="ele-fluid">
                <el-option label="自然院系" value="1"/>
                <el-option label="体育院系" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="院系:">
              <el-select
                filterable
                clearable
                v-model="where.departmentsId"
                size="small"
                placeholder="-请选择院系-"
                class="ele-block">
                <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>

                                  <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        v-if="showtable"
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
              @click="exportData"
              type="primary"
              icon="el-icon-download"
              class="ele-btn-icon"
              size="small">导出
            </el-button>
          </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            @click="openAnalysis(row)"
          >院系画像
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            @click="openagailan(row)"
          >专业概览
          </el-link>
        </template>



                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <departpj-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import DepartpjEdit from './departpj-edit';
import gradeSelect from '@/components/gradeSelect'
import projectSelect from '@/components/projectSelect'
import XLSX from 'xlsx';

export default {
  name: 'ExamDepartpj',
  components: {DepartpjEdit,gradeSelect,projectSelect},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      showtable:false,
      // 表格数据接口
      url: '/departpj/index',
      // 表格列配置
      columns: [
       /* {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        }, */
        {
          prop: 'grade_name',
          label: '年级',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'pro_name',
          label: '项目',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
        prop: 'name',
        label: '院系',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'avg',
        label: '平均分',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'goodNum',
        label: '优秀人数',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'liangNum',
        label: '良好人数',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'jigeNum',
        label: '及格人数',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'noJigeNum',
        label: '不及格人数',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'youRate',
        label: '优良率',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
      {
        prop: 'hegeRate',
        label: '合格率',
        showOverflowTooltip: true,
        minWidth: 200,
        align: 'center',
      },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      projectList:[],
      xuenianList:[],
      classList: [],
      departmentsList:[]
    };
  },
  created() {

    // 获取学年
    this.getXueList();
    // 获取班级列表
    this.getClassList();
  },
  methods: {
    /**
     * 获取院系列表
     */
    getDepartmentsList() {
      if(this.where.stype){
        delete this.where.departmentsId
        delete this.where.professionalId
        delete this.where.classId
        // this.where.departmentsId = '';
        // this.where.professionalId = '';
        // this.where.classId = '';
        // this.$forceUpdate();
      }
      this.$http.post('/departments/getDepartmentsList',{type: this.where.stype}).then(res => {
        if (res.data.code === 0) {
          this.departmentsList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    changeGrade(val){
      this.where.gradeId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    chageProject(val){
      this.where.projectId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    chageProjectId(val){
      this.where.projectId = val;
      this.$forceUpdate();
    },
    changeGradeId(val){
      this.where.gradeId = val;
      this.$forceUpdate();
    },
    openAnalysis(row){
      this.$router.push({path: '/evaluate/yuanxiAnalysis', query: {name: row.name,id:row.id}});
    },
    openagailan(row){
      this.$router.push({path: '/professpj', query: {name: row.name,id:row.id}});
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        projectId:this.where.projectId,
        gradeId:this.where.gradeId
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 导出数据 */
    exportData() {
      let array = [[ '年级','项目',  '院系', '平均分', '优秀人数', '良好人数', '及格人数', '不及格人数','优良率','合格率']];
      const loading = this.$loading({lock: true});
      this.$http.get('/departpj/index?page=1&limit=999999&projectId='+this.where.projectId+'&gradeId='+this.where.gradeId+'&annual='+this.where.annual+'&startTime='+this.where.startTime+'&endTime='+this.where.endTime+'&stype='+this.where.stype+'&departmentsId='+this.where.departmentsId).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.grade_name,
              d.pro_name,
              d.name,
              d.avg,
              d.goodNum,
              d.liangNum,
              d.jigeNum,
              d.noJigeNum,
              d.youRate,
              d.hegeRate,
             // this.$util.toDateString(d.createTime)
            ]);
          });
          this.$util.exportSheet(XLSX, array, '年级概览');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /**
        * 获取组织列表
        */
       getProjectList() {
         this.$http.get('/project/getProjectList').then(res => {
           if (res.data.code === 0) {
             this.projectList = res.data.data;
             this.where.projectId=this.projectList && this.projectList.length > 0 ? this.projectList[0].id : ''
             return this.where.projectId
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
       getXueList() {
         this.$http.get('/annual/getXueList').then(res => {
           if (res.data.code === 0) {
             this.xuenianList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /**
        * 获取班级列表
        */
       getClassList() {
         if(this.where.superior){
           this.where.class = null;
           this.$forceUpdate();
         }
         this.$http.post('/clss/getClassList',{yid:this.where.superior}).then(res => {
           if (res.data.code === 0) {
             this.classList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/departpj/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的年级院系概览吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/departpj/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/departpj/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
                          }
}
</script>

<style scoped>
</style>
